﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Analytics;

public class EditBaseInfoPanel : AppUI
{
    int m_Step;

    public EditGenderAndBirthUI birthYearGo;
    public EditEduAndLocationUI eduGo;
    public EditIconUI iconGo;

    UserBaseInfoData userBaseInfoData;

    public const int Step_BirthYear = 0;
    public const int Step_Edu = 1;
    public const int Step_Icon = 2;

    public override void Start()
    {
        base.Start();
    }

    public override void DoOnActive()
    {
        base.DoOnActive();

        GameDebug.Log($"EditBaseInfoPanel -> DoOnActive");

        Init();
    }

    void Init()
    {
        var muser = MGame.instance.MUser;

        int step = 0;
        if (muser.userInfo.birthYear <= 0)
        {
            step = Step_BirthYear;
        }
        else if (muser.userInfo.eduation <= 0)
        {
            step = Step_Edu;
        }
        else
        {
            step = Step_Icon;
        }
        m_Step = step;
        ShowObj(m_Step);

        userBaseInfoData = new UserBaseInfoData();

        birthYearGo.onEditEnd = OnBirthYearEidtEnd;
        eduGo.onEditEnd = OnEduAndLocationEidtEnd;
        iconGo.onEditEnd = OnIconEditEnd;
    }

    void OnBirthYearEidtEnd(int gender, int birthYear)
    {
        userBaseInfoData.gender = gender;
        userBaseInfoData.birthYear = birthYear;

        m_Step++;
        ShowObj(m_Step);
    }
    void OnEduAndLocationEidtEnd(int education, string locationProvince, string locationCity)
    {
        userBaseInfoData.edu = education;
        userBaseInfoData.locationProvince = locationProvince;
        userBaseInfoData.locationCity = locationCity;

        m_Step++;
        ShowObj(m_Step);
    }
    void OnIconEditEnd(Tex icon)
    {
        userBaseInfoData.icon = icon;

        PanelLoading.Show(5);
        RequestServerController.instance.ReqBaseInfo(userBaseInfoData,(ret)=>
        {
            PanelLoading.Hide();
            GoBack();
        });
    }

    void ShowObj(int step)
    {
        this.birthYearGo.gameObject.SetActive(step == Step_BirthYear);
        this.eduGo.gameObject.SetActive(step == Step_Edu);
        this.iconGo.gameObject.SetActive(step == Step_Icon);
    }


    // Update is called once per frame
    void Update()
    {

    }

    public void GoBack()
    {
        var main = UIController.ShowUI<MainPanel>();
        main.Refresh();
    }

}
